package com.nageoffer.shortLink.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortLink.admin.common.convention.result.Result;
import com.nageoffer.shortLink.admin.common.convention.result.Results;
import com.nageoffer.shortLink.admin.remote.ShortLinkActualRemoteService;
import com.nageoffer.shortLink.admin.remote.dto.req.ShortLinkBatchCreateReqDTO;
import com.nageoffer.shortLink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.nageoffer.shortLink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.nageoffer.shortLink.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import com.nageoffer.shortLink.admin.remote.dto.resp.ShortLinkBaseInfoRespDTO;
import com.nageoffer.shortLink.admin.remote.dto.resp.ShortLinkBatchCreateRespDTO;
import com.nageoffer.shortLink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortLink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import com.nageoffer.shortLink.admin.utils.EasyExcelWebUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController(value ="shortLinkControllerByAdmin")
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkActualRemoteService shortLinkActualRemoteService;

    @PostMapping("/api/short-link/admin/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
        return shortLinkActualRemoteService.createShortLink(requestParam);
    }

    @PostMapping("/api/short-link/admin/v1/create/batch")
    public void  batchcreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response){
        Result<ShortLinkBatchCreateRespDTO> shortLinkBatchCreateRespDTOResult = shortLinkActualRemoteService.batchcreateShortLink(requestParam);
        if(shortLinkBatchCreateRespDTOResult.isSuccess()){
            List<ShortLinkBaseInfoRespDTO> baseLinkInfos = shortLinkBatchCreateRespDTOResult.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response,"批量创建短链接-SaaS短链接系统",ShortLinkBaseInfoRespDTO.class,baseLinkInfos);
        }
    }

    @GetMapping("/api/short-link/admin/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        return shortLinkActualRemoteService.pageShortLink(requestParam);
    }

    @PostMapping("/api/short-link/admin/v1/update")
    public Result<Void>  updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkActualRemoteService.updateShortLink(requestParam);
        return Results.success();
    }


}
